<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="450px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="停车类型" prop="status">
        <el-radio-group v-model="form.status" @change="changeParkType">
          <el-radio
            v-for="item in parkType"
            :key="item.label"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        v-if="form.status === '月卡'"
        label="过期时间"
        prop="validDate"
        :rules="{
          required: true,
          trigger: 'blur',
          message: '请选择过期时间！',
        }"
      >
        <el-date-picker
          v-model="form.validDate"
          class="full-select"
          clearable
          placeholder="请选择过期时间"
          type="date"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button :loading="isSaving" type="primary" @click="save">
        确 定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { updateCarInfo } from '@/api/record'

  export default {
    name: 'ParkTypeEdit',
    data() {
      return {
        form: {
          status: '临时',
          residentId: null,
          validDate: null,
        },
        rules: {
          status: [
            { required: true, trigger: 'blur', message: '请选择停车类型！' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        isSaving: false,
        parkType: [
          { label: '临时', value: '临时' },
          { label: '月卡', value: '月卡' },
          { label: '长期', value: '长期' },
        ],
      }
    },
    computed: {},
    created() {},
    methods: {
      changeParkType(label) {
        if (label !== '月卡') {
          this.form.validDate = null
        }
      },
      showEdit(row) {
        this.title = '停车类型编辑'
        this.form.status = row.parkType || '临时'
        this.form.residentId = row.id
        this.form.validDate = row.validDate || null
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            try {
              this.isSaving = true
              const res = await updateCarInfo(
                this.form.residentId,
                this.form.status,
                this.form.validDate
              )
              if (res && res.status === 0) {
                this.$baseMessage(
                  '设置成功！',
                  'success',
                  'vab-hey-message-success'
                )
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(
                  res ? res.msg : '设置失败！',
                  'error',
                  'vab-hey-message-error'
                )
              }
            } catch (e) {
              this.$baseMessage(
                e ? e.message : '设置失败！',
                'error',
                'vab-hey-message-error'
              )
            }
            this.isSaving = false
          }
        })
      },
    },
  }
</script>
